package xyz.bali16.module.netdisk.controller;

import io.swagger.annotations.ApiOperation;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import xyz.bali16.application.core.exception.BusinessException;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.netdisk.controller.utils.NetdiskArticleCallObj;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.util.Optional;

import static xyz.bali16.application.core.model.Result.success;

/**
 * @author bali2 on 8/11/2021.
 * @version 1.0
 * @description 用来给网盘服务对象调用的接口控制层
 */
@RestController
@RequestMapping(BaseController.API+"caller")
public class NetdiskCallerController extends BaseController {


    //
    //@PostMapping(value = "/file/round/upload")
    //
    //@ApiOperation("轮播图上传")
    //public Result<String> roundImageUpload(MultipartFile fileBlob, HttpServletRequest request ) {
    //    return resizeImageUpload(fileBlob, request, "round", 1000, 300);
    //}

    //
    //@RequestMapping(value = "/file/download")
    //@ApiOperation("下载文件")
    //public void download(@RequestParam(value = "fileId") String fileId,
    //                     HttpServletRequest request,
    //                     HttpServletResponse response,
    //                     String netdiskObj) {
    //    articleNetdiskObj.download(fileId, request, response);
    //}
    //
    //@RequestMapping(value = "/file/delete")
    //@ApiOperation("删除上传文件")
    ////@RequiresPermissions("netdisk:caller:delete")
    //public Result<Boolean> delete(@RequestParam(value = "fileId") String fileId, String netdiskObj) {
    //    return success(articleNetdiskObj.delete(fileId));
    //}

    //@GetMapping(value = "/file/list")
    //@ApiOperation("查看附件")
    //public String fileList(PageParam param,
    //                       String netdiskObj) {
    //    NetdiskArticleCallObj articleNetdiskObj = Optional.ofNullable(NetdiskArticleCallObj.getInstance(netdiskObj)).orElseThrow(() -> new BusinessException("找不到网盘实例"));
    //    List<?> files = articleNetdiskObj.list(curPage, pageSize).getList();
    //
    //
    //}
    //
    //@GetMapping(value = "/file/list/{imgId}")
    //@ApiOperation("查看附件")
    ////@RequiresPermissions("netdisk:caller:query")
    //public String fileList(Model model,
    //                       @PathVariable("imgId") String imgId,
    //                       @RequestParam(value = "curPage", required = false, defaultValue = "1") Integer curPage,
    //                       @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
    //                       String netdiskObj) {
    //    NetdiskArticleCallObj articleNetdiskObj = Optional.ofNullable(NetdiskArticleCallObj.getInstance(netdiskObj)).orElseThrow(() -> new BusinessException("找不到网盘实例"));
    //    List<?> files = articleNetdiskObj.list(curPage, pageSize).getList();
    //    model.addAttribute("files", files);
    //    model.addAttribute("imgId", imgId);
    //    return super.render("file/index");
    //}


    
    //public Result<String> resizeImageUpload(MultipartFile fileBlob, HttpServletRequest request, String netdiskObj, Integer height, Integer weight) {
    //
    //    try {
    //        //MultipartFile 转 File
    //        File tempFile = new File(fileBlob.getOriginalFilename());
    //        tempFile.createNewFile();
    //        Thumbnails.of(fileBlob.getInputStream()).size(height, weight).keepAspectRatio(false).toFile(tempFile);
    //        //File 转 MultipartFile
    //        FileItem fileItem = null;
    //        fileItem = new DiskFileItem("mainFile", Files.probeContentType(tempFile.toPath()), false, tempFile.getName(), (int) tempFile.length(), tempFile.getParentFile());
    //        InputStream input = new FileInputStream(tempFile);
    //        try (OutputStream os = fileItem.getOutputStream()) {
    //            IOUtils.copy(input, os); // Or faster..  IOUtils.copy(new FileInputStream(file), fileItem.getOutputStream());
    //            MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
    //            tempFile.delete();
    //            return roundNetdiskObj.upload(multipartFile, request);
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
    //
    //    } catch (IOException e) {
    //        e.printStackTrace();
    //    }
    //    return Result.failure();
    //}


}
